<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\ApiResponse;
use App\Helpers\UploadHelper;
use App\Http\Controllers\Controller;
use App\Services\SysFile\SysFileService;
use Faker\Provider\Base;
use Illuminate\Http\Request;

/**
 * 系统文件控制器
 * @package App\Http\Controllers\Admin
 */
class SysFileController extends Controller
{
    use ApiResponse;
    use UploadHelper;
    private $sysFileSer;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->sysFileSer = new SysFileService();
    }

    /**
     * 文件列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $typeList = $this->sysFileSer->getTypeList();
        if ($request->ajax()) {
            $pageSize = $request->get('pageSize', 10);
            $query = \App\Models\SysFile::select('*');
            $fileType = $request->get('fileType', '');
            if ($fileType){
                $query->where('file_type', $fileType);
            }
            //搜索关键字
            $keyword = $request->get('keyword', '');
            if ($keyword) {
                $query->where('origin_name', 'like', '%' . $keyword . '%');
            }
            $data = $query->paginate($pageSize)->toArray();
            if ($data['data']){
                $resultList = $data['data'];
                foreach ($resultList as $k => $v){
                    $v['file_type_str'] = isset($typeList[$v['file_type']]) ? $typeList[$v['file_type']] : '未知';
                    $resultList[$k] = $v;
                }
                $data['data'] = $resultList;
            }
            return $data;
        }
        $data = [];
        $data['typeList'] = $typeList;
        return view('admin.sysfile.index', $data);
    }
}
